<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
define('SRT_PAGE_PARSE_START_TIME', microtime());
define('SRT_CURRENT_FILE_DIRECTORY', dirname(__FILE__) . '/');
include_once(SRT_CURRENT_FILE_DIRECTORY . 'includes/config/language.config.php');
include_once(SRT_CURRENT_FILE_DIRECTORY . 'includes/config.php');
include_once(SRT_CURRENT_FILE_DIRECTORY . 'includes/constants.php');
include_once(SRT_CURRENT_FILE_DIRECTORY . 'includes/config/cache.config.php');

//
define('SYS_RUN_REQUEST_URI', substr($_SERVER['REQUEST_URI'], strlen(CONFIG_URL_PATH)));

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//process
$error = false;
$action = (isset($_GET['action']) ? $_GET['action'] : '');
//request uri
//$request_uri = SYS_RUN_REQUEST_URI;
if( strpos(SYS_RUN_REQUEST_URI, '?') === false ) {
	$request_uri = SYS_RUN_REQUEST_URI;
} else {
	$request_uri = substr(SYS_RUN_REQUEST_URI, 0, strpos(SYS_RUN_REQUEST_URI, '?'));
}
//
if( strpos($_request_uri, '?') === false ) {
	$_request_uri = $_request_uri;
} else {
	$_request_uri = substr($_request_uri, 0, strpos($_request_uri, '?'));
}
//filter
$request_uri = ereg_replace("(\/{2,})", "/", $request_uri);
$request_uri = ereg_replace("(^\/)", "", $request_uri);
$_request_uri = ereg_replace("(\/{2,})", "/", $_request_uri);
$_request_uri = ereg_replace("(^\/)", "", $_request_uri);
//friendly
if( CONFIG_URL_DIRECTORY == true ) {
	//
} else {
	$request_uri = str_replace('~', '/', $request_uri);
}

//
if( strlen($_request_uri) > 0 && (substr($_request_uri, 0, strlen('index.php')) != 'index.php') ) {
	//parse
	$ds = explode('/', str_replace(CONFIG_URL_SUFFIX, '', $_request_uri));
	if( sizeof($ds) >= 2 ) {
		$request_type = $ds[0];
		$request_link = $ds[1];
	} elseif( sizeof($ds) == 1 ) {
		$request_type = $ds[0];
		$request_link = '';
	}
} else {
	$request_type = '';
	$request_link = '';
}
$request_path = $request_type;
$request_param = $_SERVER['QUERY_STRING'];
//
if( $request_type == '' ) {
	$request_type = 'home';
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
$cache_enabled = true;
$use_cache_request_types = array('home', 'product', 'products', 'autoitem', 'category', 'search', 'popup', 'about', 'page', 'info', 'a', 'content');
$not_cache_request_types = array('admin', 'member', 'my', 'search', 'check', 'error', 'upload', 'down', 'api', 'ws', 'xml');
if( strlen($request_type) > 0 && in_array($request_type, $use_cache_request_types) ) {
	$cache_enabled = true;
}
if( strlen($request_type) > 0 && in_array($request_type, $not_cache_request_types) ) {
	$cache_enabled = false;
}
if( isset($_POST['action']) && $_POST['action'] != '' ) {
	$cache_enabled = false;
}
if( isset($_REQUEST['edit']) || isset($_REQUEST['manage']) ) {
	$cache_enabled = false;
}
//
$filter_pages_four = strtolower(substr(SYS_RUN_REQUEST_URI, -4));
$filter_pages_five = strtolower(substr(SYS_RUN_REQUEST_URI, -5));
if( in_array($filter_pages_four, array('.jpg', '.gif', '.png', '.bmp', '.asp')) || $filter_pages_five == '.aspx' ) {
	exit();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cache_time = 0;
if( isset($CONFIG['cache']['urls']) && is_array($CONFIG['cache']['urls']) ) {
	if( $_request_uri == '' ) {
		$_request_uri = 'home';
	}
	foreach($CONFIG['cache']['urls'] as $item) {
		if( preg_match('/'.$item['pattern'].'/', $_request_uri) == true ) {
			if( $item['status'] == 0 ) {
				$cache_enabled = false;
			} else {
				$cache_enabled = true;
				$cache_time = $item['expire'];
				if( $item['loop'] == 0 ) {
					break;
				}
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
if( $cache_enabled == false ) {
	//top
	require(CONFIG_PATH . 'includes/application_top.php');
	
	//controller
	include_once(CONFIG_PATH . 'includes/controller.php');
	
	//bottom
	require(CONFIG_PATH . 'includes/application_bottom.php');
} else {
	//
	define('SYS_RT_CACHE_CONTROL', '1');
	//
	if( isset($_COOKIE['_pustatus']) && $_COOKIE['_pustatus'] == 1 ) {
		$ustatus = 1;
	} else {
		$ustatus = 0;
	}
	//
	define('SRT_CACHE_PAGES', 'html/');
	$cachepage = $request_uri;
	$cachedir = dirname($cachepage) . '/';
	$cachepage = basename($cachepage);
	$cachedir = ereg_replace("[^A-Za-z0-9_\/-]", "", $cachedir);
	$cachedir = ereg_replace("( {1,})", "-", $cachedir);
	$cachedir = trim($cachedir);
	$cachepage = str_replace(CONFIG_URL_SUFFIX, '', $cachepage);
	$cachepage = ereg_replace("[^A-Za-z0-9_-]", "", $cachepage);
	$cachepage = ereg_replace("( {1,})", "-", $cachepage);
	$cachepage = trim($cachepage);
	if( $cachepage == '' ) {
		$cachepage = 'index';
	}
	$cachepage = $cachepage . CONFIG_URL_SUFFIX;
	$cachepagefile = CONFIG_PATH . SRT_CACHE_PAGES . $cachedir . $cachepage;
	$timestamptfile = CONFIG_PATH . CONFIG_DIR_CACHE . 'timestamp.txt';
	//
	$cachepagefile_generate = true;
	if( file_exists($cachepagefile) ) {
		/*
		$fp = fopen($cachepagefile, 'r');
		$lastupdate = fread($fp, 20);
		fclose($fp);
		$lastupdate = substr($lastupdate, 5, 10);
		*/
		$timestamp = filemtime($timestamptfile);
		$lastupdate = filemtime($cachepagefile);
		
		if( $cache_time > 0 ) {
			if( $timestamp < $lastupdate && time() - $lastupdate < $cache_time ) {
				$cachepagefile_generate = false;
			}
		} elseif( CONST_CACHE_CONTROL_TIME > 0 ) {
			if( $timestamp < $lastupdate && time() - $lastupdate < CONST_CACHE_CONTROL_TIME ) {
				$cachepagefile_generate = false;
			}
		} else {
			if( $timestamp < $lastupdate ) {
				$cachepagefile_generate = false;
			}
		}
		//
		//echo $timestamp;echo '<br>';
		//echo $lastupdate;echo '<br>';
		//exit;
	}
	//
	if( $cachepagefile_generate == false ) {
		//header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
		//header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastupdate) . ' GMT');
		//header('Cache-Control: no-store, no-cache, must-revalidate');
		//header('Cache-Control: post-check=0, pre-check=0', false);
		//header('Pragma: no-cache');
		
		//include($cachepagefile);
		//$_CONTENT = file_get_contents($cachepagefile);
		//echo $_CONTENT;
		
		//echo '<!-- cached -->';
		
		//include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'gzip.function.php');
		//func_gzip_init();
		//include($cachepagefile);
		//echo '<!-- cached gzip -->';
		//func_gzip_perform();
		
		//
		header('Content-Type: text/html;charset=UTF-8');
		//header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
		//header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');
		
		include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'gzip_compression.function.php');
		func_gzip_start(1);
		include($cachepagefile);
		//echo '<!-- cached gzip compression -->';
		func_gzip_end();
		//
	} else {
		//header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
		//header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
		//header('Cache-Control: no-store, no-cache, must-revalidate');
		//header('Cache-Control: post-check=0, pre-check=0', false);
		//header('Pragma: no-cache');
		
		//
		header('Content-Type: text/html;charset=UTF-8');
		//header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
		//header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');
		
		//
		ob_start();
		//
		require(CONFIG_PATH . 'includes/application_top.php');
		
		//controller
		include_once(CONFIG_PATH . 'includes/controller.php');
		
		//
		require(CONFIG_PATH . 'includes/application_bottom.php');
		//
		
		$_CONTENT = ob_get_contents();
		ob_clean();
		
		//
		//$_CONTENT = '<!-- ' . time() . '  ' . SYS_RUN_REQUEST_URI . ' -->' . "\n" . $_CONTENT;
		//$_CONTENT .= '<!-- ' . time() . '  ' . SYS_RUN_REQUEST_URI . ' -->';
		
		//
		if( !is_dir(CONFIG_PATH . SRT_CACHE_PAGES . $cachedir) ) {
			//mkdir(CONFIG_PATH . SRT_CACHE_PAGES . $cachedir);
			util_mkdir(SRT_CACHE_PAGES . $cachedir);
		}
		
		//
		if( !isset($CONFIG['cache']['current']) || $CONFIG['cache']['current'] == 1 ) {
			$fp = fopen($cachepagefile, 'w+');
			fwrite($fp, $_CONTENT);
			fclose($fp);
		}
		
		//
		//echo $_CONTENT;
		include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'gzip_compression.function.php');
		func_gzip_start(1);
		echo $_CONTENT;
		//echo '<!-- gzip compression -->';
		func_gzip_end();
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
$time_start = explode(' ', SRT_PAGE_PARSE_START_TIME);
$time_end = explode(' ', microtime());
$parse_time = number_format(($time_end[1] + $time_end[0] - ($time_start[1] + $time_start[0])), 3);
echo '<div id="pagetime" class="hidden">' . $parse_time . '</div>';
//foreach($_COOKIE as $k => $v) {
//	echo $k . '=' . $v;echo '<br>';
//}
//echo func_db_get_query_count();
//echo '<div class="clear"><div>';
//d($adb->getQueryLog());
//echo '<div class="clear"></div><pre>';print_r($_SESSION);echo '</pre>';
//echo '<pre>';print_r($_SESSION);echo '</pre>';
//echo '<pre>';print_r($GLOBALS);echo '</pre>';
//
?>